package com.ruoyi.web.utils.tecentCos;


import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.bouncycastle.asn1.bc.ObjectData;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.util.Date;

/**
 * 阿里云上传控件
 *
 * @author DSL
 * @date 2019/10/12
 */
@Component
public class TecentOssService {

    /**
     * 初始化secretId
     */
    private String secretId = "AKIDscDblhV5RecNuELI1VmpgJjxxkIbgYHn";

    /**
     * secretKey
     */
    private String secretKey = "OZYKsCMH9nBTZUEl37ya2GMKDuOefR0M";
    /**
     * 区域
     */
    private Region region = new Region("ap-beijing");
    /**
     * 指定文件将要存放的存储桶
     */
    private String bucketName = "aibaby-1255582383";
    /**
     * 文件目录
     */
    private String folder = "source/";
    /**
     * 返回链接
     */
    private String resultUrl = "https://aibaby-1255582383.cos.ap-beijing.myqcloud.com/";


    /**
     * 以文件流的方式上传文件
     *
     * @param file 文件
     * @return
     * @Author: DSL
     */
    public String uploadFile(File file,String newFileName) {
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        // 指定要上传的文件
        // 指定文件将要存放的存储桶
        // 指定文件上传到 COS 上的路径，即对象键。例如对象键为folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
        String resultName=null;
//        if (StringUtils.isNotBlank(newFileName))
//        {
//            resultName=newFileName;
//        }
//        else {
            String fileName = file.getName();
//            String[] names = fileName.split(".");
            resultName = String.valueOf(new Date().getTime())+fileName;
//        }
        String key = folder + resultName;
        com.qcloud.cos.model.PutObjectRequest putObjectRequest = new com.qcloud.cos.model.PutObjectRequest(bucketName, key, file);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        //                String fileUrl = aliyunOssService.uploadFile(file);
        String results = resultUrl + key;

        return results;
    }

    public String uploadFileInput(MultipartFile file, String newFileName) {
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        // 指定要上传的文件
        // 指定文件将要存放的存储桶
        // 指定文件上传到 COS 上的路径，即对象键。例如对象键为folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
        String resultName=null;
        String fileName = file.getOriginalFilename();
        resultName = String.valueOf(new Date().getTime())+fileName;

        String key = folder + resultName;

        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        }catch (Exception e) {
            e.printStackTrace();
        }

        ObjectMetadata objectMetadata = new ObjectMetadata();

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, objectMetadata);
        cosClient.putObject(putObjectRequest);
//        com.qcloud.cos.model.PutObjectRequest putObjectRequest = new com.qcloud.cos.model.PutObjectRequest(bucketName, key, file);
//        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        String results = resultUrl + key;

        return results;
    }
}